package com.poker.wiki.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.poker.common.exception.BusinessException;
import com.poker.common.exception.BusinessExceptionCode;
import com.poker.common.utils.CopyUtil;
import com.poker.common.utils.RedisUtil;
import com.poker.common.utils.RequestContext;
import com.poker.common.utils.SnowFlake;
import com.poker.wiki.Service.DocService;
import com.poker.wiki.entity.Content;
import com.poker.wiki.entity.Doc;
import com.poker.wiki.entity.DocExample;
import com.poker.wiki.mapper.ContentMapper;
import com.poker.wiki.mapper.DocMapper;
import com.poker.wiki.req.DocQueryReq;
import com.poker.wiki.req.DocSaveReq;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DocServiceImpl implements DocService {

    @Resource
    DocMapper docMapper;

    @Resource
    SnowFlake snowFlake;

    @Resource
    ContentMapper contentMapper;

    @Resource
    RedisUtil redisUtil;

    @Override
    public List<Doc> getAllDoc() {
        DocExample example = new DocExample();
        example.setOrderByClause(" sort asc ");
        return docMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Doc> findDoc(DocQueryReq req) {
        DocExample example = new DocExample();
        DocExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%"+req.getName()+"%");
        }

        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<Doc> docList = docMapper.selectByExample(example);
        PageInfo<Doc> pageInfo = new PageInfo<>(docList);
        return pageInfo;
    }

    @Override
    public void delete(Long id) {
        docMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(DocSaveReq req) {
        Content content = new Content();
        Doc doc = CopyUtil.copy(req, Doc.class);
        content.setContent(req.getContent());
        if (ObjectUtils.isEmpty(doc.getId())) {
            doc.setId(snowFlake.nextId());
            docMapper.insert(doc);

            content.setId(doc.getId());
            contentMapper.insert(content);
        } else {
            docMapper.updateByPrimaryKey(doc);
            content.setId(doc.getId());
            int count = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (count == 0 ) {
                content.setId(doc.getId());
                contentMapper.insert(content);
            }
        }
    }

    @Override
    public List<Doc> getDocByEbookId(Long ebookId) {
        DocExample example = new DocExample();
        example.setOrderByClause(" sort asc ");
        example.createCriteria().andEbookIdEqualTo(ebookId);

        return docMapper.selectByExample(example);
    }

    @Override
    public void updateDocView(Long id) {
        docMapper.updateDocView(id);
    }

    @Override
    public void docVote(Long id) {
//        docMapper.updateDocVote(id);

        String ip = RequestContext.getRemoteAddr();
        boolean boo = redisUtil.validateRepeat(ip + "_" + id, 3600*24);
        docMapper.updateDocVote(id, boo);
        if (!boo) {
            redisUtil.delelteRepeat(ip + "_" + id);
            throw new BusinessException(BusinessExceptionCode.VOTE_CANCEL);
        }

    }

    @Override
    public boolean docVoteStatus(Long id) {
        String ip = RequestContext.getRemoteAddr();
        return redisUtil.validateRepeat(ip + "_" + id);
    }

    @Override
    public void updateEbookInfo() {
        docMapper.updateEbookInfo();
    }

}
